package com.dsx.gyj.servlet;

import com.dsx.gyj.entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * author:xiaoxue1272
 * date:2020/5/17-18:17
 */
@WebServlet("/update")
public class UpdateServlet extends OurServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        User user = super.cr.selectOne(id);
        req.setCharacterEncoding("utf-8");
        req.setAttribute("user",user);
        req.setAttribute("url","update");
        req.getRequestDispatcher("data.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User user = new User();
        req.setCharacterEncoding("utf-8");
        user.setId(Integer.parseInt(req.getParameter("id")));
        user.setName(req.getParameter("name"));
        user.setSex(req.getParameter("sex"));
        user.setAge(Integer.parseInt(req.getParameter("age")));
        super.cr.update(user);
        resp.sendRedirect("/");
    }
}